<template>
  <div class="login_container">

    <div class="login_box">

      <div class="avatar_box">
        <img src="@/assets/logo.png" />
      </div>

      <el-form
        ref="form"
        :model="form"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!--用户代码-->
        <el-form-item prop="userCode">

          <el-input prefix-icon="el-icon-user"
                    placeholder="请输入用户名"
                    v-model="form.userCode">
          </el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="密码"
            v-model="form.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      form: {
        userCode: "",
        password: ""
      },
      loginFormRules: {
          userCode: [
          { required: true, message: "用户名必填", trigger: "blur" },
          { min: 4, max: 8, message: "用户名在4到8个字符之间", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码必填", trigger: "blur" },
          {
            min: 2,
            max: 8,
            message: "用户密码在4到8个字符之间",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    resetLogin() {
      this.$refs.form.resetFields();
    },
    login() {
          this.$refs.form.validate(valid => {
          if (!valid) return;
          let url = this.$baseUrl+"/manager/login"
          this.axios.post(url,{
              userCode:this.form.userCode ,
              password:this.form.password,
            }).then((res)=>{
              console.log(res.data);
              if (res.data.code == 200) {
                  this.$message.success("登陆成功")
                  //保存用户信息
                  sessionStorage.setItem("userCode",this.form.userCode);
                  //访问主页
                  this.$router.push("/home");
              }else {
                  this.$message.error("登陆失败")
              }
          })
          });
    },

    // login() {
    //     this.$refs.from.validate(valid =>{
    //         if ((!valid))return;
    //         let url = "http://localhost:8081/manager/login"
    //         let params = this.qs.stringify(this.form);
    //         this.axios.post(url, params)
    //             .then((res) =>{
    //
    //             if(res.data.code==200){
    //                 this.$message.success("成功");
    //             }else {
    //                 this.$message.error("shibai")
    //             }
    //         })
    //
    //     })
    //   this.$router.push("/home")
    // },
  }
};
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 100px;
    width: 100px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -30%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>